package com.pan.admin.module.api.service.impl;

import com.gitee.apanlh.util.algorithm.digest.MD5;
import com.pan.admin.common.utils.DateUtils;
import com.pan.admin.common.utils.SecurityUtils;
import com.pan.admin.module.api.dao.ApiDataExchangeBacklistDao;
import com.pan.admin.module.api.entity.ApiDataExchangeBacklist;
import com.pan.admin.module.api.service.ApiDataExchangeBacklistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * DataExchangeBacklistService业务层处理
 *
 * @author ZJQ
 * @date 2024-10-25
 */
@Service
public class ApiDataExchangeBacklistServiceImpl implements ApiDataExchangeBacklistService {

    @Autowired
    private ApiDataExchangeBacklistDao apiDataExchangeBacklistDao;

    @Override
    public List<ApiDataExchangeBacklist> list(ApiDataExchangeBacklist apiDataExchangeBacklist) {
        //  存放用于查询的手机号MD5
        List<String> phoneMd5List = new ArrayList<>();
        //  去除数据中的数组为null的情况
        if (apiDataExchangeBacklist.getPhoneMultiple() == null) {
            apiDataExchangeBacklist.setPhoneMultiple(new ArrayList<>());
        }
        if (apiDataExchangeBacklist.getPhoneMd5Multiple() == null) {
            apiDataExchangeBacklist.setPhoneMd5Multiple(new ArrayList<>());
        }
        //  将apiDataExchangeBacklist中的数据加入数组中
        if (apiDataExchangeBacklist.getPhone() != null)  {
            apiDataExchangeBacklist.getPhoneMultiple().add(apiDataExchangeBacklist.getPhone());
        }
        if (apiDataExchangeBacklist.getPhoneMd5() != null) {
            apiDataExchangeBacklist.getPhoneMd5Multiple().add(apiDataExchangeBacklist.getPhoneMd5());
        }

        //  phone数组去重
        Set<String> setPhone = new HashSet<>(apiDataExchangeBacklist.getPhoneMultiple());
        apiDataExchangeBacklist.setPhoneMultiple(new ArrayList<>(setPhone));
        //  phoneMd5数组去重
        Set<String> setPhoneMd5 = new HashSet<>(apiDataExchangeBacklist.getPhoneMd5Multiple());
        apiDataExchangeBacklist.setPhoneMd5Multiple(new ArrayList<>(setPhoneMd5));

        //  查询数组
        List<ApiDataExchangeBacklist> selectList = new ArrayList<>();

        //  根据手机号数组新增撞库黑名单
        for (String phone : apiDataExchangeBacklist.getPhoneMultiple()) {
            ApiDataExchangeBacklist backlist = new ApiDataExchangeBacklist();
            backlist.setPhone(phone);
            backlist.setPhoneMd5(MD5.create().digestToHex(phone));
            selectList.add(backlist);
            //  将手机号对应的手机号MD5再在手机号MD5数组中删除，防止重复
            apiDataExchangeBacklist.getPhoneMd5Multiple().remove(backlist.getPhoneMd5());
        }

        //  新增手机号MD5数组中的数据
        for (String phoneMd5 : apiDataExchangeBacklist.getPhoneMd5Multiple()) {
            ApiDataExchangeBacklist backlist = new ApiDataExchangeBacklist();
            backlist.setPhoneMd5(phoneMd5);
            selectList.add(backlist);
        }

        for (ApiDataExchangeBacklist backlist : selectList) {
            if (!backlist.getPhoneMd5().isEmpty()){
                phoneMd5List.add(backlist.getPhoneMd5());
            }
        }
        return apiDataExchangeBacklistDao.list(phoneMd5List);
    }

    @Override
    @Transactional
    public int insetApiDataExchangeBacklist(ApiDataExchangeBacklist apiDataExchangeBacklist) {
        //  设置创建人
        apiDataExchangeBacklist.setCreateBy(SecurityUtils.getUsername());

        //  去除数据中的数组为null的情况
        if (apiDataExchangeBacklist.getPhoneMultiple() == null) {
            apiDataExchangeBacklist.setPhoneMultiple(new ArrayList<>());
        }
        if (apiDataExchangeBacklist.getPhoneMd5Multiple() == null) {
            apiDataExchangeBacklist.setPhoneMd5Multiple(new ArrayList<>());
        }

        //  phone数组去重
        Set<String> setPhone = new HashSet<>(apiDataExchangeBacklist.getPhoneMultiple());
        apiDataExchangeBacklist.setPhoneMultiple(new ArrayList<>(setPhone));
        //  phoneMd5数组去重
        Set<String> setPhoneMd5 = new HashSet<>(apiDataExchangeBacklist.getPhoneMd5Multiple());
        apiDataExchangeBacklist.setPhoneMd5Multiple(new ArrayList<>(setPhoneMd5));

        //  新增数组
        List<ApiDataExchangeBacklist> addList = new ArrayList<>();
        //  根据手机号数组新增撞库黑名单
        for (String phone : apiDataExchangeBacklist.getPhoneMultiple()) {
            ApiDataExchangeBacklist backlist = new ApiDataExchangeBacklist();
            backlist.setPhone(phone);
            backlist.setPhoneMd5(MD5.create().digestToHex(phone));
            backlist.setCreateBy(SecurityUtils.getUsername());
            backlist.setCreateTime(DateUtils.getNowDate());
            addList.add(backlist);
            //  将手机号对应的手机号MD5再在手机号MD5数组中删除，防止重复
            apiDataExchangeBacklist.getPhoneMd5Multiple().remove(backlist.getPhoneMd5());
        }

        //  新增手机号MD5数组中的数据
        for (String phoneMd5 : apiDataExchangeBacklist.getPhoneMd5Multiple()) {
            ApiDataExchangeBacklist backlist = new ApiDataExchangeBacklist();
            backlist.setPhoneMd5(phoneMd5);
            backlist.setCreateBy(SecurityUtils.getUsername());
            backlist.setCreateTime(DateUtils.getNowDate());
            addList.add(backlist);
        }

        //  数据库中查重
        List<String> phoneMd5List = new ArrayList<>();
        for (ApiDataExchangeBacklist backlist : addList) {
            if (backlist.getPhoneMd5() != null){
                phoneMd5List.add(backlist.getPhoneMd5());
            }
        }
        List<ApiDataExchangeBacklist> selectList = apiDataExchangeBacklistDao.list(phoneMd5List);

        //  更新数据
        for (ApiDataExchangeBacklist item : addList){
            for (ApiDataExchangeBacklist backlist : selectList) {
                if (Objects.equals(item.getPhoneMd5(), backlist.getPhoneMd5())) {
                    if (item.getPhone() != null && backlist.getPhone() == null) {
                        item.setId(backlist.getId());
                        item.setUpdateTime(DateUtils.getNowDate());
                        item.setUpdateBy(SecurityUtils.getUsername());
                        apiDataExchangeBacklistDao.updateApiDataExchangeBacklist(item);
                    }
                }
            }
        }
        //  根据是否重复将需要新增的数据放入list集合中暂存
        List<ApiDataExchangeBacklist> list = new ArrayList<>();
        for (ApiDataExchangeBacklist item : addList){
            Boolean flag = true;
            for (ApiDataExchangeBacklist backlist : selectList) {
                if (Objects.equals(item.getPhoneMd5(), backlist.getPhoneMd5())) {
                    flag = false;
                }
            }
            if (flag){
                list.add(item);
            }
        }

        if (!list.isEmpty()) {
            return apiDataExchangeBacklistDao.insetApiDataExchangeBacklist(list);
        }
        return 1;
    }

    @Override
    public int deleteById(Long id) {
        return apiDataExchangeBacklistDao.deleteById(id);
    }
}
